<?php

namespace Beerus\Modules\Super\Controllers;

use App\Pockets\GridDataModel as pGridDataModel;
use App\Http\Controllers\Controller;
use hhz\constant\redis\redisKeys;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Biz\cache\activity\SuperBuyerPlanetMess;
use Encore\Admin\Widgets\Form;

/**
 * 阿里云虚拟手机号的号池管理
 *
 * @category   Module name
 */
class SuperBuyerController extends Controller
{
    const PAGE_SIZE = 20;
    const BIND_DIRECTIONS = [
        1 => 'UXO',
        2 => 'OXU',
    ];

    public function __construct()
    {
        parent::__construct();
        //隐藏新增按钮--避免隐藏动画出现
        Admin::style('.grid-create-btn{display:none;}');
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    public function index(Request $request, Content $content)
    {
        $info = [];
        $start_time =  SuperBuyerPlanetMess::getTimeItem("start_time");
        $end_time =  SuperBuyerPlanetMess::getTimeItem("end_time");
        if($start_time && $end_time){
            $info['start_time'] = date( "Y-m-d H:i:s",$start_time);
            $info['end_time'] =  date( "Y-m-d H:i:s" ,$end_time);
        }

        $form = new Form($info);
        $form->datetime('start_time', '开始时间')->width("300px")->required();
        $form->datetime('end_time', '到期时间')->width("300px")->required();
        $form->disableReset();
        $form->action('/admin/super/save');
        return $content

            ->row($form);
    }

    public function save(Request $request)
    {
        try {
            $request = $request->all();

            if ($request['start_time'] && $request['end_time']) {
                SuperBuyerPlanetMess::setTimeItems("start_time" , strtotime($request['start_time']));

                SuperBuyerPlanetMess::setTimeItems("end_time" , strtotime($request['end_time']));

                $status = true;
                $message = '操作成功';
            } else {
                $status = false;
                $message = '操作失败';
            }
        } catch (\Exception $e) {
            $status = false;
            $message = $e->getMessage();
        }
        return redirect('/admin/super/index')->json(compact('status', 'message'));
    }

}
